# -*- coding: utf-8 -*-
from five import grok
from zope import schema
from plone.directives import form
from zope.interface import Invalid
from plone.app.textfield import RichText
from plone.namedfile.field import NamedImage
from plone.namedfile.interfaces import IImageScaleTraversable
from z3c.relationfield.schema import RelationList, RelationChoice
from plone.formwidget.contenttree import ObjPathSourceBinder,ContentTreeFieldWidget

from Products.bpContents import _
from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
from plone.formwidget.autocomplete import AutocompleteFieldWidget
from collective.z3cform.widgets.token_input_widget import TokenInputFieldWidget
from Products.ATVocabularyManager import NamedVocabulary
from collective.gtags.field import Tags
from plone.indexer import indexer
from Products.CMFCore.utils import getToolByName
from Products.bpContents.adapter import KeywordSourceBinder
from plone.formwidget.autocomplete import AutocompleteMultiFieldWidget


class IGaleria(form.Schema,IImageScaleTraversable):
    """El tipo de contenido GALERIA, se utiliza tanto para crear un espacio donde poner fotos, como una sala o como 
    """

    cuerpo = RichText(
        title=_(u"Cuerpo"),
        description=_(u"Texto principal"),
    )  

    form.order_before(tipoGaleria='IBasic.description')
    tipoGaleria=schema.Choice(
       title=_(u"Tipo Galería"),
       vocabulary=SimpleVocabulary(
        [SimpleTerm(value=u'Multimedia', title=_(u'Multimedia')),
        SimpleTerm(value=u'Sala Museo', title=_(u'Sala Museo'))]
        ),
    )


    form.widget(categorias=AutocompleteMultiFieldWidget)
    #categorias =schema.Choice(title=u"Categorias",source=KeywordSourceBinder())
    categorias = schema.List(title=u"Tematicas",value_type=schema.Choice(title=u"Categorias",source=KeywordSourceBinder()),required=False)


    form.fieldset('Bio',
            label=u"Bio del personaje",
            fields=['nombre', 'bio' ,'retrato'],
            required=False)
    nombre = schema.TextLine(title=u"Nombre",
            description=_(u"Biografía del personaje, OBLIGATORIO si la galeria es una Sala Museo"),
            required=False,
    )
    bio = RichText(
        title=_(u"Biografía"),
        description=_(u"Biografía del personaje"),
        required=False,
    )
    retrato = NamedImage(
         title=_(u"Retrato"),
         description=_(u"Retrato del personaje, es OBLIGATORIO si la galeria es una Sala Museo"),
         required=False,
    )
    relaciones = RelationList(
        title=_(u"Documentos Relacionados"),
        description=_(u"Otros articulos o galerias que complementen el documento actual"),
        value_type=RelationChoice(
            source=ObjPathSourceBinder(
            ),
        ),
        required=False,
    )

@indexer(IGaleria)
def startIndexer(context):
    if context.categorias is None:
        return None

    atvm = getToolByName(context , 'portal_vocabularies')
    engines = atvm.getVocabularyByName('categorias_vocab')
    result=[]

    for clave in context.categorias:
        result.append(clave.split(":")[0])
    return result

grok.global_adapter(startIndexer, name="articuloCategoria")


from Acquisition import aq_inner
from plone.directives.dexterity import DisplayForm
from Products.bpContents.BaseClassView import BaseClassView
class View(DisplayForm,BaseClassView):
    grok.context(IGaleria)
    grok.require('zope2.View')


    def getImages(self):
        """Toda las imagenes"""
        from Products.bpContents.getImgTira import GetTiraImgs
        dummyTira = GetTiraImgs(self.context)
        results = dummyTira.getTira()

        if results:
            return results
        else:
            return None

    def needCollapseBio(self):
        """ """
        ret=False
        if len(self.context.bio.output)>520:
            ret=True

        return ret


    def getCreatorInfo(self):
        """Devuelve los datos del responsable del artículo"""
        from Products.bpContents.CreatorInfo import CreatorInfo
        dataMember=CreatorInfo(self.context)
        return dataMember.getCreatorInfo()